package edu.dongthang.screens;

import java.util.Locale;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

import edu.dongthang.screens.R;
import edu.dongthang.controller.Touch;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;

public class MainMenu extends Activity {

	WifiManager mainWifi;
	BluetoothAdapter bluetoothadapter;
	AlertDialog alert_exit;
	AlertDialog alert_wifi;
	AlertDialog alert_bluetooth;
	Configuration config;
	Button wifi;
	//Button bluetooth;
	Button setting;
	Button exit;
	Button help;
	Typeface font4;
	private AdView adView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_mainmenu);
		setContentView(R.layout.activity_mainmenu);
		font4 = Typeface.createFromAsset(getAssets(),"fonts/HLT bauserif.ttf");
		
		wifi = (Button) findViewById(R.id.btn_wifi);
		//bluetooth = (Button) findViewById(R.id.btn_bluetooth);
		setting = (Button) findViewById(R.id.btn_setting);
		exit = (Button) findViewById(R.id.btn_exit);
		help = (Button)findViewById(R.id.btn_help);
		
		//set custom font
		wifi.setTypeface(font4);
		//bluetooth.setTypeface(font4);
		setting.setTypeface(font4);
		exit.setTypeface(font4);
		help.setTypeface(font4);
		
		adView = (AdView) this.findViewById(R.id.adView);
        adView.loadAd(new AdRequest()); 
		
		wifi.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
		//bluetooth.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
		setting.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
		exit.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
		help.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
		
		
		final String wifi_message = getResources().getString(R.string.wifi_off_message);
		String bluetooth_message = getResources().getString(R.string.bluetooth_off_message);
		String exit_app = getResources().getString(R.string.exit_app);
		String ok = getResources().getString(R.string.ok);
		String cancel = getResources().getString(R.string.cancel);
		
		
		
		
		mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		bluetoothadapter = BluetoothAdapter.getDefaultAdapter();

		alert_exit = new AlertDialog.Builder(this).create();
		alert_exit.setTitle(R.string.exit);
		alert_exit.setMessage(exit_app);
		alert_exit.setIcon(R.drawable.icon_exit);
		
		alert_exit.setButton3(ok, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				System.exit(0);
			}
		});
		alert_exit.setButton(cancel, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		
		
		alert_wifi = new AlertDialog.Builder(this).create();
		alert_wifi.setTitle(R.string.wifi_off);
		alert_wifi.setMessage(wifi_message);
		alert_wifi.setIcon(R.drawable.warning_icon);
		alert_wifi.setButton(ok, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				return;
			}
		});
		
		
		alert_bluetooth = new AlertDialog.Builder(this).create();
		alert_bluetooth.setTitle(R.string.bluetooth_off);
		alert_bluetooth.setMessage(bluetooth_message);
		alert_bluetooth.setIcon(R.drawable.warning_icon);
		alert_bluetooth.setButton(ok, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				return;
			}
		});

		// button wifi
		wifi.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				Intent menu = new Intent(MainMenu.this, Touch.class);
				if (mainWifi.isWifiEnabled() == false) {
					//alert_wifi.show();
					Toast.makeText(getBaseContext(),wifi_message.toString(), Toast.LENGTH_SHORT).show();
					startActivity(menu);
				} else {					
					startActivity(menu);
				}
			}
		});
		
		wifi.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    //Button Pressed
                	wifi.setBackgroundResource(R.drawable.bg_button_hover);
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    wifi.setBackgroundResource(R.drawable.bg_button_press);
                }
                return false;
            }
        });
		

		// bluetooth exit
//		bluetooth.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				if (bluetoothadapter == null) {
//					Toast.makeText(getApplicationContext(),
//							"Your device not support Bluetooth",
//							Toast.LENGTH_SHORT).show();
//
//				}
//				if (!bluetoothadapter.isEnabled()) {
//					alert_bluetooth.show();
//				} else {
//					Toast.makeText(getApplicationContext(),
//							"Open bluetooth activity", Toast.LENGTH_SHORT)
//							.show();
//				}
//
//			}
//		});
		
//		bluetooth.setOnTouchListener(new OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if(event.getAction() == MotionEvent.ACTION_DOWN){
//                    //Button Pressed
//                	bluetooth.setBackgroundResource(R.drawable.bg_button_press);
//                }
//                if(event.getAction() == MotionEvent.ACTION_UP){
//                	bluetooth.setBackgroundResource(R.drawable.bg_button_hover);
//                }
//                return false;
//            }
//        });
		
		// button setting
		setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent setting = new Intent(MainMenu.this, SettingScreen.class);
				startActivity(setting);
			}
		});
		
		setting.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    //Button Pressed
                	setting.setBackgroundResource(R.drawable.bg_button_press);
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                	setting.setBackgroundResource(R.drawable.bg_button_hover);
                }
                return false;
            }
        });

		// button exit
		exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alert_exit.show();

			}
		});
		
		
		exit.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    //Button Pressed
                	exit.setBackgroundResource(R.drawable.bg_button_press);
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                	exit.setBackgroundResource(R.drawable.bg_button_hover);
                }
                return false;
            }
        });
		
		
		help.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainMenu.this,HelpScreens.class);
			    startActivity(intent);
			}
		});
		
		help.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    //Button Pressed
                	help.setBackgroundResource(R.drawable.bg_button_press);
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                	help.setBackgroundResource(R.drawable.bg_button_hover);
                }
                return false;
            }
        });
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onResume();
		updateResourse();	
		refresh();
	}
	@Override
    public void onDestroy() {
      if (adView != null) {
        adView.destroy();
      }
      super.onDestroy();
    }  
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	private void refresh() {
		// TODO Auto-generated method stub
		finish();
        Intent myIntent = new Intent(MainMenu.this, MainMenu.class);
        startActivity(myIntent);
	}
	private void updateResourse() {
		config = new Configuration();
		config.locale = Locale.getDefault();
		getBaseContext().getResources().updateConfiguration(config,
				getBaseContext().getResources().getDisplayMetrics());
	}
}
